package com.qianjiangcloud.store.util;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qianjiangcloud.store.Run;
import com.qianjiangcloud.store.activity.AgentActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import com.qianjiangcloud.store.R;
import com.qianjiangcloud.store.libary.cache.MyImageLoader;

/**
 * 商品展示栏
 */

/***
 * @author wangguodong
 *         优化嵌套列表显示问题  废除原有的recyclerview嵌套实现
 *         原有实现卡顿严重
 */

@SuppressWarnings("unused")
public class GoodsDisplayUtils {

    private LinearLayout itemLayout = null;

    public View getGoodsDisplayItem(Context mContext) {
        itemLayout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.goods_display_item, null);
        return itemLayout;
    }


    public void setUpview(int index, String arrss, Context mContext, View view, String title, String title_color, String title_icon, String isshow_module_title) {

        LinearLayout isshow_title = (LinearLayout) view.findViewById(R.id.isshow_title);


        if (isshow_module_title.equals("false")) {
            isshow_title.setVisibility(View.GONE);
        }

        if (index == 0 && isshow_module_title.equals("true")) {
            isshow_title.setVisibility(View.VISIBLE);
        }
        else{
            isshow_title.setVisibility(View.GONE);
        }


        TextView title_name = (TextView) view.findViewById(R.id.title);
        ImageView icon = (ImageView) view.findViewById(R.id.image_icon);
        ImageView icon2 = (ImageView) view.findViewById(R.id.image_icon2);
        ImageView title_image = (ImageView) view.findViewById(R.id.image);

        if (null != title) {
            title_name.setText(title);
        }
        if (null != title_color) {
            title_name.setTextColor(Color.parseColor(title_color));
            icon.setBackgroundColor(Color.parseColor(title_color));
            icon2.setBackgroundColor(Color.parseColor(title_color));
        }
        if (null != title_icon) {
            MyImageLoader.displayDefaultImage(title_icon, title_image);
        }


        try {
            JSONObject all = new JSONObject(arrss);
            JSONArray arr = all.optJSONObject("data").optJSONArray("info");

            generateGoodsItem(arr, mContext, view, index);


        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    private void generateGoodsItem(JSONArray json_str, Context mContext, View view, int index) {

        ArrayList<JSONObject> mShowGoodsArray = new ArrayList<JSONObject>();

        if (json_str != null && json_str.length() > 0) {
            mShowGoodsArray.clear();


            for (int i = 2 * index; i < json_str.length() && i < 2 * index + 2; i++) {
                JSONObject topJson = json_str.optJSONObject(i);
                mShowGoodsArray.add(topJson);
            }
            if (mShowGoodsArray.size() > 0) {
                reloadMainView(false, mContext, mShowGoodsArray, view);
            } else {
                reloadMainView(true, mContext, mShowGoodsArray, view);
            }
        }


    }


    private void reloadMainView(boolean isOutDate, final Context mContext, final ArrayList<JSONObject> mShowGoodsArray, View view) {

        if (mShowGoodsArray != null && mShowGoodsArray.size() > 0 && !isOutDate) {


            LinearLayout ll_item = (LinearLayout) view.findViewById(R.id.ll_item);
            ImageView ll_iv = (ImageView) view.findViewById(R.id.goods_show_item_image_ll);
            TextView ll_sale_price = (TextView) view.findViewById(R.id.sale_price_ll);
            TextView ll_pay_count = (TextView) view.findViewById(R.id.pay_count_ll);
            TextView ll_item_text = (TextView) view.findViewById(R.id.item_text_ll);


            LinearLayout rl_item = (LinearLayout) view.findViewById(R.id.rl_item);
            ImageView rl_iv = (ImageView) view.findViewById(R.id.goods_show_item_image_rl);
            TextView rl_sale_price = (TextView) view.findViewById(R.id.sale_price_rl);
            TextView rl_pay_count = (TextView) view.findViewById(R.id.pay_count_rl);
            TextView rl_item_text = (TextView) view.findViewById(R.id.item_text_rl);



            ll_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    JSONObject obj = mShowGoodsArray.get(0);
                    if (obj == null) {
                        return;
                    }
                    String goodsIID = obj.optString("goods_id");
                    Intent intent = AgentActivity.intentForFragment(
                            mContext, AgentActivity.FRAGMENT_GOODS_DETAIL)
                            .putExtra(Run.EXTRA_CLASS_ID, goodsIID);
                    mContext.startActivity(intent);

                }
            });


            rl_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    JSONObject obj = mShowGoodsArray.get(1);
                    if (obj == null) {
                        return;
                    }
                    String goodsIID = obj.optString("goods_id");
                    Intent intent = AgentActivity.intentForFragment(
                            mContext, AgentActivity.FRAGMENT_GOODS_DETAIL)
                            .putExtra(Run.EXTRA_CLASS_ID, goodsIID);
                    mContext.startActivity(intent);

                }
            });

            if (mShowGoodsArray.size() == 1) {

                rl_item.setVisibility(View.INVISIBLE);
                ll_item.setVisibility(View.VISIBLE);

                ll_item_text.setText(mShowGoodsArray.get(0).optString("good_title")+"\n");
                ll_pay_count.setText(mShowGoodsArray.get(0).optString("pay_count") + "人付款");
                ll_sale_price.setText("￥"+mShowGoodsArray.get(0).optString("sale_price"));
                MyImageLoader.displayDefaultImage(mShowGoodsArray.get(0).optString("good_img"), ll_iv);
            } else if (mShowGoodsArray.size() >= 2) {
                rl_item.setVisibility(View.VISIBLE);
                ll_item.setVisibility(View.VISIBLE);

                ll_item_text.setText(mShowGoodsArray.get(0).optString("good_title")+"\n");
                ll_pay_count.setText(mShowGoodsArray.get(0).optString("pay_count") + "人付款");
                ll_sale_price.setText("￥"+mShowGoodsArray.get(0).optString("sale_price"));
                MyImageLoader.displayDefaultImage(mShowGoodsArray.get(0).optString("good_img"), ll_iv);

                rl_item_text.setText(mShowGoodsArray.get(1).optString("good_title")+"\n");
                rl_pay_count.setText(mShowGoodsArray.get(1).optString("pay_count") + "人付款");
                rl_sale_price.setText("￥"+mShowGoodsArray.get(1).optString("sale_price"));
                MyImageLoader.displayDefaultImage(mShowGoodsArray.get(1).optString("good_img"), rl_iv);
            } else {
                rl_item.setVisibility(View.INVISIBLE);
                ll_item.setVisibility(View.INVISIBLE);
                view.setVisibility(View.GONE);
            }


        } else {
            view.setVisibility(View.GONE);
        }




    }

//    private class DataAdapter extends RecyclerView.Adapter {
//
//        private LayoutInflater mLayoutInflater;
//        private ArrayList<JSONObject> mDataList = new ArrayList();
//
//        public DataAdapter(Context context) {
//            mLayoutInflater = LayoutInflater.from(context);
//        }
//
//        private void addAll(ArrayList<JSONObject> list) {
//            int lastIndex = this.mDataList.size();
//            if (this.mDataList.addAll(list)) {
//                notifyItemRangeInserted(lastIndex, list.size());
//            }
//        }
//
//        @Override
//        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//            return new ViewHolder(mLayoutInflater.inflate(R.layout.show_goods_item, parent, false));
//        }
//
//        @Override
//        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//
//            JSONObject item = mDataList.get(position);
//
//            ViewHolder viewHolder = (ViewHolder) holder;
//            viewHolder.textView.setText(item.optString("good_title"));
//            viewHolder.cost_price.setText("￥" + item.optString("cost_price"));
//            viewHolder.pay_count.setText(item.optString("pay_count") + "人付款");
//            viewHolder.sale_price.setText(item.optString("sale_price"));
//            viewHolder.cost_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);  // 设置中划线并加清晰
//           /* Uri imageUri = Uri.parse(item.optString("good_img"));
//            viewHolder.iv.setImageURI(imageUri);*/
//            MyImageLoader.displayDefaultImage(item.optString("good_img"), viewHolder.iv);
//
//        }
//
//        @Override
//        public int getItemCount() {
//            return mDataList.size();
//        }
//
//        private class ViewHolder extends RecyclerView.ViewHolder {
//
//            private TextView textView;
//            private LinearLayout ll;
//            private ImageView iv;
//            private TextView sale_price;
//            private TextView cost_price;
//            private TextView pay_count;
//
//            public ViewHolder(final View itemView) {
//                super(itemView);
//                ll = (LinearLayout) itemView.findViewById(R.id.ll_show_goods_item);
//                textView = (TextView) itemView.findViewById(R.id.goods_gallery_item_text);
//                sale_price = (TextView) itemView.findViewById(R.id.sale_price);
//                cost_price = (TextView) itemView.findViewById(R.id.cost_price);
//                pay_count = (TextView) itemView.findViewById(R.id.pay_count);
//                iv = (ImageView) itemView.findViewById(R.id.goods_show_item_image);
//                ll.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
////                       String text = mDataList.get(RecyclerViewUtils.getAdapterPosition(mRecyclerView, ViewHolder.this)).optString("good_title");
////                        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
//                        JSONObject obj = mDataList.get(RecyclerViewUtils.getAdapterPosition(mRecyclerView, GoodsDisplayUtils.DataAdapter.ViewHolder.this));
//                        if (obj == null) {
//                            return;
//                        }
////                        Log.i("intent",obj+"");
//                        String goodsIID = obj.optString("goods_id");
//                        Intent intent = AgentActivity.intentForFragment(
//                                itemView.getContext(), AgentActivity.FRAGMENT_GOODS_DETAIL)
//                                .putExtra(Run.EXTRA_CLASS_ID, goodsIID);
//                        itemView.getContext().startActivity(intent);
//
//                    }
//                });
//            }
//        }
//    }

}
